﻿using Microsoft.Extensions.Configuration;
using Publicuse.Entity.sqlsugar;
using Publicuse.Interface;
using Publicuse.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Service
{
    public class MySqlSugarService : BaseSqlSugarService, IMySqlSugarService
    {
        public MySqlSugarService(ISqlSugarClient sugarClient, IConfiguration configuration) : base(sugarClient)
        {
            var mysql = new CustomConnectionConfig();
            configuration.Bind("MySqlConnectionString", mysql);
            ConnectionConfig connectionConfig = new ConnectionConfig()
            {
                ConfigId = "MySql",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,         
                ConnectionString = AesEncryptionUtil.AESDecrypt(mysql.MasterConnectionString, out var msg),
                //SlaveConnectionConfigs = mysql.SlaveConnectionString.Select(c => new SlaveConnectionConfig()
                //{
                //    ConnectionString = AesEncryptionUtil.AESDecrypt(c.ConnectionString, out var msg),
                //    HitRate = c.HitRate
                //}).ToList()
            };
            base.sugarClient= new SqlSugarScope(connectionConfig);
            //base.sugarClient = new SqlSugarScope(connectionConfig);
            //调试SQL事件，可以删掉
            //base.sugarClient.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    Console.WriteLine($"Sql语句：{sql}");
            //    Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数 
            //};

            #region SQL执行前
            base.sugarClient.Aop.OnLogExecuting = (sql, pars) =>
            {
                //Console.WriteLine(sql);//输出sql
                if (pars != null)
                {
                    //Console.WriteLine(JSON.Serialize(pars.Select(it => it.ParameterName + ":" + it.Value)));//输出参数
                } 
            };
            #endregion

            #region SQL执行完
            base.sugarClient.Aop.OnLogExecuted = (sql, pars) =>                                                                                                                             
            {
                //执行完了可以输出SQL执行时间 (OnLogExecutedDelegate) 
                //Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());
            };
            #endregion

            #region SQL报错
            base.sugarClient.Aop.OnError = (exp) =>//SQL报错
            {
                //exp.sql 这样可以拿到错误SQL，性能无影响拿到ORM带参数使用的SQL
                //exp.Sql.LogError<SqlString>();
                //5.0.8.2 获取无参数化 SQL  对性能有影响，特别大的SQL参数多的，调试使用
                //Console.WriteLine(UtilMethods.GetSqlString(DbType.Oracle, exp.Sql, exp.Parametres as SugarParameter[]));
            };
            #endregion
        }
    }

    public class MySqlSugarbService : BaseSqlSugarService, IMySqlSugarbService
    {
        public MySqlSugarbService(ISqlSugarClient sugarClient, IConfiguration configuration) : base(sugarClient)
        {
            var mysql = new CustomConnectionConfig();
            configuration.Bind("MySqlConnectionStringb", mysql);
            ConnectionConfig connectionConfig = new ConnectionConfig()
            {
                ConfigId = "MySql",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                ConnectionString = AesEncryptionUtil.AESDecrypt(mysql.MasterConnectionString, out var msg),
                SlaveConnectionConfigs = mysql.SlaveConnectionString.Select(c => new SlaveConnectionConfig()
                {
                    ConnectionString = AesEncryptionUtil.AESDecrypt(c.ConnectionString, out var msg),
                    HitRate = c.HitRate,
                }).ToList()
            };
            //base.sugarClient= new SqlSugarClient(connectionConfig);
            base.sugarClient = new SqlSugarScope(connectionConfig);
            //调试SQL事件，可以删掉
            //base.sugarClient.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    Console.WriteLine($"Sql语句：{sql}");
            //    Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数 
            //};
        }
    }
}
    